<?php
/**
 * Created by PhpStorm.
 * User: 13790
 * Date: 2019/11/2
 * Time: 16:30
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Test extends Controller
{
    public function appdowload(){
        return $this->fetch ('user/appddd');
    }
    public function test(){
        $uid = input('uid');
        $uname = input('uname');
        $title = input('title');
        $image = input('image');
        $real_pay = input('real_pay');
        $red_give = input('red_give');
        $all_pay = input('all_pay');
        $give_jifen = input('give_jifen');
        $data = ['uid' => $uid,'uname' => $uname,'title' => $title,'image' => $image,'real_pay' => $real_pay,'red_give' => $red_give,'all_pay' => $all_pay,'give_jifen' => $give_jifen];
        $this->assign('data',$data);
        $configImg = Db::name('jifen_conf')
            ->where(['conf_id'=>24])
            ->value ('conf_img');
        $this->assign('configImg',$configImg);
        $shop_id  = 0;
        if($uid && $uid > 0){
            $mwwsid = Db::name('shop')->where('shop_user_id',$uid)->value('shop_id');
            if($mwwsid) $shop_id = $mwwsid;
        }
        $this->assign('shop_id',$shop_id);
        return $this->fetch ('user/test');
    }
    public function test2(){
        $uid = input('uid');
        $uname = input('uname');
        $title = input('title');
        $image = input('image');
        $real_pay = input('real_pay');
        $red_give = input('red_give');
        $all_pay = input('all_pay');
        $give_jifen = input('give_jifen');
        $data = ['uid' => $uid,'uname' => $uname,'title' => $title,'image' => $image,'real_pay' => $real_pay,'red_give' => $red_give,'all_pay' => $all_pay,'give_jifen' => $give_jifen];
        $this->assign('data',$data);
        $configImg = Db::name('jifen_conf')
            ->where(['conf_id'=>24])
            ->value ('conf_img');
        $this->assign('configImg',$configImg);
        $shop_id  = 0;
        if($uid && $uid > 0){
            $mwwsid = Db::name('shop')->where('shop_user_id',$uid)->value('shop_id');
            if($mwwsid) $shop_id = $mwwsid;
        }
        $this->assign('shop_id',$shop_id);
        return $this->fetch ('user/test2');
    }
    public function testtest(){
        header("Access-Control-Allow-Origin:*");
        $record_number = $_POST['out_trade_no'];
        $msg = Db::name('record')
            ->where(['record_number'=>$record_number])
            ->field ('record_xiaopiao,record_ru_id,record_money,record_status')
            ->find();
        if($msg['record_ru_id'] > 0){
            $redid = Db::name('m_red_user')
                ->where(['ru_id'=>$msg['record_ru_id']])
                ->value ('ru_red_id');
            $image = Db::name('m_red')
                ->where(['red_id'=>$redid])
                ->value ('red_image');
        }else{
            $image = Db::name('jifen_conf')
                ->where(['conf_id'=>24])
                ->value ('conf_img');
        }
        return json(['msg' => $msg['record_xiaopiao'],'image' => $image,'money' => $msg['record_money'],'record_status' => $msg['record_status']]);
    }

    public function get_sign(){
        // 微信 JS 接口签名校验工具: https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
        $appid = 'wxcb63bdccb205e77b';
        $secret = 'd9903a933268a1452b9471e6fb5586be';
        // 获取token
        $token_data = Db::table('up_token')->where('id',1)->find();
        $time  = time() - $token_data['time'];
        if ($time > 3600) {
            $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
            $token_res = $this->https_request($token_url);
            $token_res = json_decode($token_res, true);
//            dump($token_res);die;
            $token = $token_res['access_token'];
            $data = array(
                'time' =>time(),
                'token' =>$token
            );
            Db::table('up_token')->where('id',1)->update($data);
        } else {
            $token = $token_data['token'];
        }
        // 获取ticket
        $ticket_data = Db::table('up_token')->where('id',2)->find();
        $time1  = time() - $ticket_data['time'];
        if ($time1 > 3600) {
            $ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$token}&type=jsapi";
            $ticket_res = $this->https_request($ticket_url);
            $ticket_res = json_decode($ticket_res, true);
            $ticket = $ticket_res['ticket'];
            $data = array(
                'time'    =>time(),
                'token'  =>$ticket
            );
            Db::table('up_token')->where('id',2)->update($data);
        } else {
            $ticket = $ticket_data['token'];
        }
        // 进行sha1签名
        $timestamp = time();
        $nonceStr = $this->createNonceStr();
        // 注意 URL 建议动态获取(也可以写死).
        $uid = input('uid');
        $uname = urlencode(input('uname'));
        $title = urlencode(input('title'));
        $image = urlencode(input('image'));
        $real_pay = input('real_pay');
        $red_give = input('red_give');
        $all_pay = input('all_pay');
        $give_jifen = input('give_jifen');
        $url = 'https://csdl.jintianjifen.cn/admin/test/test?uid=' . $uid.'&uname=' . $uname.'&title=' . $title.'&image=' . $image.'&real_pay=' . $real_pay.'&red_give=' . $red_give.'&all_pay=' . $all_pay.'&give_jifen=' . $give_jifen; // 调用JSSDK的页面地址
        //$url = $_SERVER['HTTP_REFERER']; // 前后端分离的, 获取请求地址(此值不准确时可以通过其他方式解决)
        $str = "jsapi_ticket={$ticket}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$url}";
        $sha_str = sha1($str);
        $signPackage = array(
            "appId" =>  $appid,
            "nonceStr" => $nonceStr,
            "timestamp" => $timestamp,
            "signature" => $sha_str,
        );
        return json(['status'=>200,'msg'=>$signPackage,'url'=>$url]);
    }
    public function get_sign2(){
        // 微信 JS 接口签名校验工具: https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
        $appid = 'wxcb63bdccb205e77b';
        $secret = 'd9903a933268a1452b9471e6fb5586be';
        // 获取token
        $token_data = Db::table('up_token')->where('id',1)->find();
        $time  = time() - $token_data['time'];
        if ($time > 3600) {
            $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
            $token_res = $this->https_request($token_url);
            $token_res = json_decode($token_res, true);
//            dump($token_res);die;
            $token = $token_res['access_token'];
            $data = array(
                'time' =>time(),
                'token' =>$token
            );
            Db::table('up_token')->where('id',1)->update($data);
        } else {
            $token = $token_data['token'];
        }
        // 获取ticket
        $ticket_data = Db::table('up_token')->where('id',2)->find();
        $time1  = time() - $ticket_data['time'];
        if ($time1 > 3600) {
            $ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$token}&type=jsapi";
            $ticket_res = $this->https_request($ticket_url);
            $ticket_res = json_decode($ticket_res, true);
            $ticket = $ticket_res['ticket'];
            $data = array(
                'time'    =>time(),
                'token'  =>$ticket
            );
            Db::table('up_token')->where('id',2)->update($data);
        } else {
            $ticket = $ticket_data['token'];
        }
        // 进行sha1签名
        $timestamp = time();
        $nonceStr = $this->createNonceStr();
        // 注意 URL 建议动态获取(也可以写死).
        $uid = input('uid');
        $uname = urlencode(input('uname'));
        $title = urlencode(input('title'));
        $image = urlencode(input('image'));
        $real_pay = input('real_pay');
        $red_give = input('red_give');
        $all_pay = input('all_pay');
        $give_jifen = input('give_jifen');
        $url = 'https://csdl.jintianjifen.cn/admin/test/test2?uid=' . $uid.'&uname=' . $uname.'&title=' . $title.'&image=' . $image.'&real_pay=' . $real_pay.'&red_give=' . $red_give.'&all_pay=' . $all_pay.'&give_jifen=' . $give_jifen; // 调用JSSDK的页面地址
        //$url = $_SERVER['HTTP_REFERER']; // 前后端分离的, 获取请求地址(此值不准确时可以通过其他方式解决)
        $str = "jsapi_ticket={$ticket}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$url}";
        $sha_str = sha1($str);
        $signPackage = array(
            "appId" =>  $appid,
            "nonceStr" => $nonceStr,
            "timestamp" => $timestamp,
            "signature" => $sha_str,
        );
        return json(['status'=>200,'msg'=>$signPackage,'url'=>$url]);
    }
    //随机字符串
    public function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
    // 模拟 http 请求
    public function https_request($url, $data = null){
        // curl 初始化
        $curl = curl_init();
        // curl 设置
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        // 判断 $data get  or post
        if ( !empty($data) ) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // 执行
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
}